﻿using System;
using DataStore;
using DataStore.Domain;
using DataStore.Repositories;
using Infrastructure.CurrentData;
using Microsoft.Practices.Composite.Events;

namespace Infrastructure.ViewModels
{
    /// <summary>
    /// View model for Cover entity
    /// </summary>
    public class CoverViewModel : DataModelSingle<Cover>
    {
        

        public CoverViewModel(Cover cover)
            : base(cover)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="CoverViewModel"/> class.
        /// </summary>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="cover">The cover.</param>
        public CoverViewModel(IEventAggregator eventAggregator, Cover cover) : base(eventAggregator, cover)
        { }

        /// <summary>
        /// Gets the current cover.
        /// </summary>
        /// <value>The cover.</value>
        public Cover Cover
        {
            get
            {
                return Item;
            }
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public Guid Id
        {
            get
            {
                return Item.Id;
            }
            set
            {
                if (Item.Id != value)
                {
                    Item.Id = value;
                    OnPropertyChanged("Id");
                }
            }
        }
        /// <summary>
        /// Gets or sets the path.
        /// </summary>
        /// <value>The path.</value>
        public LinkViewModel Path
        {
            get
            {
                var linkVM =  new LinkViewModel(EventAggregator, Item.Path);
                // TODO possible memory leak, cache can be a solution
                linkVM.PropertyChanged += delegate
                                              {
                                                  OnPropertyChanged("Path");
                                              };
                return linkVM;
            }
            set
            {
                if(Item.Path != value.Link)
                {
                    Item.Path = value.Link;
                    OnPropertyChanged("Path");
                }
            }
        }

        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        /// <value>The type.</value>
        public CoverType Type
        {
            get
            {
                return Item.Type;
            }
            set
            {
                if(Item.Type != value)
                {
                    Item.Type = value;
                    OnPropertyChanged("Type");
                }
            }
        }
        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        /// <value>The size.</value>
        public CoverSize Size
        {
            get
            {
                return Item.Size;
            }
            set
            {
                if(Item.Size != value)
                {
                    Item.Size = value;
                    OnPropertyChanged("Size");
                }
            }
        }
        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get
            {
                return Item.Description;
            }
            set
            {
                if(Item.Description != value)
                {
                    Item.Description = value;
                    OnPropertyChanged("Description");
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is default.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is default; otherwise, <c>false</c>.
        /// </value>
        public bool IsDefault
        {
            get
            {
                return Item.IsDefault;
            }
            set
            {
                if(Item.IsDefault != value)
                {
                    Item.IsDefault = value;
                    OnPropertyChanged("IsDefault");
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether this cover is new.
        /// </summary>
        /// <value><c>true</c> if this instance is new; otherwise, <c>false</c>.</value>
        protected override bool IsNewItem
        {
            get { return Id == Guid.Empty; }
        }

        /// <summary>
        /// Saves this cover.
        /// </summary>
        public override void Save()
        {
            IDatabase database = Current.Container.Resolve<IDatabase>();
            using (var unitOfWork = database.CreateUnitOfWork())
            {

                try
                {
                    IRepository<Cover> repository = new CoverRepository(unitOfWork);
                    unitOfWork.BeginTransaction();

                    repository.SaveOrUpdate(Item);

                    unitOfWork.CommitTransaction();
                }
                catch
                {
                    unitOfWork.RollbackTransaction();
                    throw;
                }
            }

            base.Save();
        }

        /// <summary>
        /// Gets a value indicating whether this cover can be save.
        /// </summary>
        /// <value><c>true</c> if this cover can be save; otherwise, <c>false</c>.</value>
        protected override bool CanSave
        {
            get
            {
                return true;
            }
        }

        

    }
}